package data;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fileProcessing.ParsingException;

public class UrlEntry extends AbstractDataType<UrlEntry>{

	private String url;
	private String fieldType = "URL";
	
	public UrlEntry(){};
	
	public UrlEntry(RandomAccessFile raf) throws ParsingException{
		try {
			url = raf.readUTF();
		} catch (IOException e) {
			throw new ParsingException("Error: "+e.getMessage()+" when reading date from table's data file");
		}
	}
	
	public UrlEntry(String newUrl) throws ParsingException{
		if(isValid(newUrl)){
			url = newUrl;
		}
		else
			throw new ParsingException("Phone number not valid.");
	}	

	public boolean isValid(String val){
		Pattern pattern = Pattern.compile("((http|ftp|https):\\/\\/)?[\\w\\-_]+(\\." +
				"[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?",Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(val);
		return matcher.matches();
	}
	
	@Override
	public int compareTo(UrlEntry o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType() {
		return fieldType;
	}

}
